/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */

package com.hohai.river.controller;

import cn.hutool.core.lang.tree.Tree;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.admin.api.entity.SysMenu;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.hohai.river.entity.SysIndex;
import com.hohai.river.service.SysIndexService;
import com.pig4cloud.pig.common.security.util.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 指标表
 *
 * @author pig code generator
 * @date 2022-09-17 10:10:19
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/sysindex")
@Tag(name = "指标表管理")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class SysIndexController {

	private final SysIndexService sysIndexService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param sysIndex 指标表
	 * @return
	 */
	@Operation(summary = "分页查询", description = "分页查询")
	@GetMapping("/page")
	@PreAuthorize("@pms.hasPermission('river_sysindex_get')")
	public R getSysIndexPage(Page page, SysIndex sysIndex) {
		return R.ok(sysIndexService.page(page, Wrappers.query(sysIndex)));
	}

	/**
	 * 通过id查询指标表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id查询", description = "通过id查询")
	@GetMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('river_sysindex_get')")
	public R getById(@PathVariable("id") Long id) {
		return R.ok(sysIndexService.getById(id));
	}

	/**
	 * 新增指标表
	 * @param sysIndex 指标表
	 * @return R
	 */
	@Operation(summary = "新增指标表", description = "新增指标表")
	@SysLog("新增指标表")
	@PostMapping
	@PreAuthorize("@pms.hasPermission('river_sysindex_add')")
	public R save(@RequestBody SysIndex sysIndex) {
		return R.ok(sysIndexService.save(sysIndex));
	}

	/**
	 * 修改指标表
	 * @param sysIndex 指标表
	 * @return R
	 */
	@Operation(summary = "修改指标表", description = "修改指标表")
	@SysLog("修改指标表")
	@PutMapping
	@PreAuthorize("@pms.hasPermission('river_sysindex_edit')")
	public R updateById(@RequestBody SysIndex sysIndex) {
		return R.ok(sysIndexService.updateById(sysIndex));
	}

	/**
	 * 通过id删除指标表
	 * @param id id
	 * @return R
	 */
	@Operation(summary = "通过id删除指标表", description = "通过id删除指标表")
	@SysLog("通过id删除指标表")
	@DeleteMapping("/{id}")
	@PreAuthorize("@pms.hasPermission('river_sysindex_del')")
	public R removeById(@PathVariable Long id) {
		return R.ok(sysIndexService.removeIndexById(id));
	}

	/**
	 * 返回树形指标集合
	 * @return 树形指标
	 */
	@GetMapping(value = "/tree/filter")
	public R<List<Tree<Long>>> getTreeFiltered(@RequestParam("riverId") Long riverId,
			@RequestParam("expert") Boolean expert) {
		return R.ok(sysIndexService.treeIndexFiltered(riverId, expert));
	}

	/**
	 * 返回树形指标集合
	 * @return 树形指标
	 */
	@GetMapping(value = "/tree")
	public R<List<Tree<Long>>> getTree() {
		return R.ok(sysIndexService.treeIndex());
	}

	/**
	 * 根据indexCode查询指标信息
	 * @return 树形指标
	 */
	@GetMapping(value = "/info")
	public R getInfoByIndexCode(@RequestParam("indexCode") String indexCode) {
		return R.ok(sysIndexService.getInfoByIndexCode(indexCode));
	}

}
